﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Com.CMS263.BLL;
using Com.CMS263.PO;
using System.IO;

namespace Com.CMS263.Web.manage
{
    public partial class manage_page : System.Web.UI.Page
    {
        AboutHelpBLL bll = new AboutHelpBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }
        public void BindData()
        {
            string type = trblist.SelectedValue.ToString();
            AboutHelp entity = new AboutHelp();
            entity = bll.GetAboutHelpByTypeAndTitle(3, type);
           
            try
            {
                FileInfo fi = new FileInfo(Server.MapPath("~/upfile/" + entity.contents));
                if (fi.Exists)
                {
                    img_show.ImageUrl = "/upfile/" + entity.contents;
                    txtImg.Text = entity.contents;
                }
                else
                {
                    img_show.Visible = false;
                }
            }
            catch (Exception)
            {
                img_show.Visible = false;

            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            AboutHelp entity = new AboutHelp();
            entity = bll.GetAboutHelpByTypeAndTitle(3, trblist.SelectedValue);
            entity.contents = txtImg.Text;
           
            if (bll.ModifyAboutHelp(entity))
            {
                ScriptManager.RegisterClientScriptBlock(this, GetType(), "js", "alert('修改成功')", true);
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, GetType(), "js", "alert('修改失败')", true);
            }
        }
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            txtImg.Text = UploadFile();
            img_show.ImageUrl = "/upfile/" + txtImg.Text;
            img_show.Visible = true;
        }
        public string UploadFile()
        {
            //检查上传文件
            if (this.FileUpload1.PostedFile.ContentLength > 0)
            {


                // path = path.Substring(6);

                string strSave = "~/upfile/";
                string strName = FileUpload1.PostedFile.FileName;

                //取得文件名(包括路径)里最后一个"."的索引

                int intExt = strName.LastIndexOf(".");

                //取得文件扩展名
                string strExt = strName.Substring(intExt);

                if (strExt.ToLower() != ".jpg" && strExt.ToLower() != ".gif" && strExt.ToLower() != ".jpeg")
                {
                    RegisterStartupScript("UserNote", "<Script   language='javascript'>alert('对不起，您只能上传.jpg,gif,ipeg格式的图片文件！');</Script>");
                    return "";
                }

                DateTime dt = DateTime.Now;

                string strNewName = "" + dt.Year + dt.Month + dt.Day + dt.Hour + dt.Minute + dt.Second + dt.Millisecond + strExt;


                FileUpload1.PostedFile.SaveAs(Server.MapPath(strSave + strNewName));
                RegisterStartupScript("ok", "<Script   language='javascript'>alert('上传成功');</Script>");
                return strNewName;

            }
            else
            {
                return "";
                RegisterStartupScript("FileNull", "<Script   language='javascript'>alert('请确认上传的文件存在或文件的大小不为0');</Script>");
            }
            this.FileUpload1.Dispose();

        }
        protected void trblist_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindData();
        }
    }
}
